import json
import os

from multipledispatch import dispatch
from config import BASE_PATH


"""
    # File       : read_file.py
    # Time       ：2023/10/12 13:51
    # Author     ：曾伟雄
    # version    ：V1.0.0
    # Description：读取文件工具类
"""
class ReadFile():

    # 读取json数据 方法1
    @classmethod
    @dispatch(type,str, str)
    def read_json(cls,filename, key) -> list:
        filePath = BASE_PATH + os.sep + "data" + os.sep + filename
        with open(filePath, encoding="utf-8") as f:
            # 读取json数据
            json_data = json.load(f)  #数据 [[],[]]
            return json_data.get(key)

    # 读取json 方法2
    @classmethod
    @dispatch(type,str)
    def read_json(cls,filename) -> list:
        filePath = BASE_PATH + os.sep + "data" + os.sep + filename
        with open(filePath, encoding="utf-8") as f:
            # 读取json数据
            json_data = json.load(f)  # 数据 [[],[]]
            return json_data

    # 写入json
    @classmethod
    def write_json(cls,filename, data):
        filepath = BASE_PATH + os.sep + "data" + os.sep + filename
        with open(filepath, "w", encoding="utf-8")as f:
            json.dump(data, f, ensure_ascii=False, indent=2)

if __name__ == "__main__":
    # pass
    print(ReadFile.read_json("parsing.json"))
    print(ReadFile.read_json("sniffing.json", "accessibility"))